package cn.consult.sys.service.impl;

import cn.consult.common.utils.GetStringParseDate;
import cn.consult.sys.dao.TCheckEquipmentDao;
import cn.consult.sys.dao.TPlaceBuildingDao;
import cn.consult.sys.dao.TPlaceFloorDao;
import cn.consult.sys.dao.TPlaceRoomDao;
import cn.consult.sys.entity.*;
import cn.consult.sys.service.TCheckEquipmentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (TCheckEquipment)表服务实现类
 *
 * @author makejava
 * @since 2024-04-02 19:09:14
 */
@Service("tCheckEquipmentService")
public class TCheckEquipmentServiceImpl extends ServiceImpl<TCheckEquipmentDao, TCheckEquipment> implements TCheckEquipmentService {

    @Autowired
    private TPlaceFloorDao tPlaceFloorDao;
    @Autowired
    private TPlaceBuildingDao tPlaceBuildingDao;
    @Autowired
    private TPlaceRoomDao tPlaceRoomDao;


    @Override
    public boolean saveCheckFlag(TCheckEquipment tCheckEquipment) throws ParseException {
        LocalDate now = LocalDate.now();
        tCheckEquipment.setCreateTime(now.toString());
        tCheckEquipment.setStatus(String.valueOf(1));
        String address = null;
        //如果是roomId就要查询他的楼层和楼栋
        if (tCheckEquipment.getRoomId() != null) {
            TPlaceRoom tPlaceRoom = tPlaceRoomDao.selectById(tCheckEquipment.getRoomId());
            TPlaceFloor tPlaceFloor = tPlaceFloorDao.selectById(tPlaceRoom.getRoomFloorid());
            TPlaceBuilding tPlaceBuilding = tPlaceBuildingDao.selectById(tPlaceFloor.getFloorBuildingid());

            address = tPlaceBuilding.getBuildingName() + "/" + tPlaceFloor.getFloorName() + "/" + tPlaceRoom.getRoomName();
            tCheckEquipment.setFloorId(Integer.parseInt(String.valueOf(tPlaceFloor.getId())));
            tCheckEquipment.setBulidingId(Integer.parseInt(String.valueOf(tPlaceBuilding.getId())));
            tCheckEquipment.setObjName(address);
        } else if (tCheckEquipment.getFloorId() != null) {
            //如果是floorId就要查询他的楼层
            TPlaceFloor tPlaceFloor = tPlaceFloorDao.selectById(tCheckEquipment.getFloorId());

            TPlaceBuilding tPlaceBuilding = tPlaceBuildingDao.selectById(tPlaceFloor.getFloorBuildingid());
            tCheckEquipment.setBulidingId(Integer.parseInt(String.valueOf(tPlaceBuilding.getId())));
            address = tPlaceBuilding.getBuildingName() + "/" + tPlaceFloor.getFloorName();
            tCheckEquipment.setObjName(address);
        } else if (tCheckEquipment.getBulidingId() != null) {
            TPlaceBuilding tPlaceBuilding = tPlaceBuildingDao.selectById(tCheckEquipment.getBulidingId());
            address = tPlaceBuilding.getBuildingName();
            tCheckEquipment.setObjName(address);
        }

        int insert = this.baseMapper.insert(tCheckEquipment);
        return insert > 0 ? true : false;
    }

    @Override
    public List<TCheckEquipment> getCheckAll(Map<String, Object> params) {
        LambdaQueryWrapper<TCheckEquipment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(params.get("bulidingId") != "", TCheckEquipment::getBulidingId, params.get("bulidingId"));
        queryWrapper.eq(params.get("floorId") != "", TCheckEquipment::getFloorId, params.get("floorId"));
        queryWrapper.eq(params.get("roomId") != "", TCheckEquipment::getRoomId, params.get("roomId"));
        queryWrapper.eq(params.get("equipmentName") != "" && params.get("equipmentName") != null, TCheckEquipment::getEquipmentName, params.get("equipmentName"));
        queryWrapper.eq(params.get("serialNumber") != "" && params.get("serialNumber") != null, TCheckEquipment::getSerialNumber, params.get("serialNumber"));

        String address = null;
        List<TCheckEquipment> equipments = this.baseMapper.selectList(queryWrapper);

        return equipments;
    }

    @Override
    public boolean updateChecklFlag(TCheckEquipment tCheckEquipment) {
        LocalDate now = LocalDate.now();
        tCheckEquipment.setCreateTime(now.toString());
        String address = null;
        //如果是roomId就要查询他的楼层和楼栋
        if (tCheckEquipment.getRoomId() != null) {
            TPlaceRoom tPlaceRoom = tPlaceRoomDao.selectById(tCheckEquipment.getRoomId());
            TPlaceFloor tPlaceFloor = tPlaceFloorDao.selectById(tPlaceRoom.getRoomFloorid());
            TPlaceBuilding tPlaceBuilding = tPlaceBuildingDao.selectById(tPlaceFloor.getFloorBuildingid());

            address = tPlaceBuilding.getBuildingName() + "/" + tPlaceFloor.getFloorName() + "/" + tPlaceRoom.getRoomName();
            tCheckEquipment.setFloorId(Integer.parseInt(String.valueOf(tPlaceFloor.getId())));
            tCheckEquipment.setBulidingId(Integer.parseInt(String.valueOf(tPlaceBuilding.getId())));
            tCheckEquipment.setObjName(address);
        } else if (tCheckEquipment.getFloorId() != null) {
            //如果是floorId就要查询他的楼层
            TPlaceFloor tPlaceFloor = tPlaceFloorDao.selectById(tCheckEquipment.getFloorId());

            TPlaceBuilding tPlaceBuilding = tPlaceBuildingDao.selectById(tPlaceFloor.getFloorBuildingid());
            tCheckEquipment.setBulidingId(Integer.parseInt(String.valueOf(tPlaceBuilding.getId())));
            address = tPlaceBuilding.getBuildingName() + "/" + tPlaceFloor.getFloorName();
            tCheckEquipment.setObjName(address);
        } else if (tCheckEquipment.getBulidingId() != null) {
            TPlaceBuilding tPlaceBuilding = tPlaceBuildingDao.selectById(tCheckEquipment.getBulidingId());
            address = tPlaceBuilding.getBuildingName();
            tCheckEquipment.setObjName(address);
        }

        int update = this.baseMapper.updateById(tCheckEquipment);
        return update > 0 ? true : false;
    }
}

